package hz.mobileproject.pda.service;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.IBinder;
import android.os.SystemClock;

import com.blankj.utilcode.util.DeviceUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.TimeUtils;

import org.xutils.common.util.LogUtil;

import hz.mobileproject.pda.entity.request.Request_heartbeat;
import hz.mobileproject.pda.listener.CustomNetCallBack;
import hz.mobileproject.pda.network.Constance;
import hz.mobileproject.pda.receiver.AlarmReceiver;
import hz.mobileproject.pda.ui.activity.TestSettingActivity;
import hz.mobileproject.pda.utils.HttpRqUtils;

public class ReportHearBeatService extends Service {

    AlarmManager manager;
    PendingIntent pi;
    public int anHour; //记录间隔时间
    private SPUtils spUtils;
    private WifiManager mWifiManager;


    public ReportHearBeatService() {
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);

    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        //调用闹钟服务(闹钟服务再次开次开启服务)
//        LogUtil.e("心跳服务开启");
        manager = (AlarmManager) getSystemService(ALARM_SERVICE);

        try {
            Intent i = new Intent(this,AlarmReceiver.class);
            intent.setAction("repeating");
            long firstTime = SystemClock.elapsedRealtime();
            pi = PendingIntent.getBroadcast(this,0,i, PendingIntent.FLAG_CANCEL_CURRENT);
            manager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime,1 * 1000,pi);
        }catch (Exception e) {
            LogUtil.e("心跳服务开启异常");
        }



        new Thread(
                new Runnable() {
                    @Override
                    public void run() {
                        Request_heartbeat requestHeartbeat = new Request_heartbeat();
                        spUtils = SPUtils.getInstance("app_login");
                        requestHeartbeat.setCus_number(spUtils.getString("cusnumber"));
                        requestHeartbeat.setPolice_id(spUtils.getString("police_id"));
                        requestHeartbeat.setPolice_name(spUtils.getString("police_name"));
                        requestHeartbeat.setDept_id(spUtils.getString("department_id"));
                        requestHeartbeat.setDept_name(spUtils.getString("department_name"));
                        requestHeartbeat.setTer_mac(DeviceUtils.getMacAddress());
                        String mac = null;
                        mWifiManager = (WifiManager)ReportHearBeatService.this.getSystemService(WIFI_SERVICE);
                        if(mWifiManager.isWifiEnabled()) {
                            WifiInfo wifiInfo = mWifiManager.getConnectionInfo();
                            //获取被连接网络的mac地址
                            mac = wifiInfo.getBSSID();
                        }
                        requestHeartbeat.setAp_mac(mac);
                        WifiInfo wifiInfo = mWifiManager.getConnectionInfo();
                        int ipAddress = wifiInfo.getIpAddress();
                        String ip = intToIp(ipAddress);
                        requestHeartbeat.setIp(ip);
                        requestHeartbeat.setTer_time(TimeUtils.getNowString());
                        requestHeartbeat.setTer_ver(getVersion());
                        HttpRqUtils.httpPost(Constance.BASE_URL + Constance.REPORT_HEART_BEAT,requestHeartbeat, new CustomNetCallBack() {
                            @Override
                            public void onSuccessFromSer(int rtnCode, String rtnMsg, String data) {
                                LogUtil.e(data);
                            }

                            @Override
                            public void onErrorFromSer(int rtnCode, String rtnMsg) {

                            }

                            @Override
                            public void onErrorFromNet(Throwable ex, boolean isOnCallback) {

                            }
                        });

                    }
                }
        ).start();




        return super.onStartCommand(intent, flags, startId);
    }


    private String intToIp(int i) {

        return (i & 0xFF ) + "." +
                ((i >> 8 ) & 0xFF) + "." +
                ((i >> 16 ) & 0xFF) + "." +
                ( i >> 24 & 0xFF) ;
    }


    private String getVersion() {
        try {
            //获取软件版本号，对应AndroidManifest.xml下android:versionCode
//            String versionCode = getPackageManager().
//                    getPackageInfo(getPackageName(), 0).versionName;
            String versionCode = TestSettingActivity.NOW_VERSION;
            return versionCode;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }

    }
}
